const fs = require('fs');
const path = require('path');  

export function saveToFiles(data, unit) {
 
let contentToAppend = `
    export const data_${Date.now()} = ${JSON.stringify(data)}
`;
 
// 文件路径
const dirPath = './app/api/discovery/tests';  

const filePath = path.join(dirPath, unit+'.ts');  

// 追加内容到文件
return  new Promise((res)=>{
    if(!fs.existsSync(dirPath)){
        fs.mkdirSync(dirPath, { recursive: true });  
    }
    let content;
    if(fs.existsSync(filePath)){
        content = fs.readFileSync(filePath, 'utf-8' );
    }
    console.log(content, 'rrrrr')
    fs.appendFile(filePath, contentToAppend, { flag: 'a' }, (err) => {
        if (err) throw err;
        console.log('内容已追加到文件！');
        res({code: 200})
      });
})

}